﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPinyin;
using System.Net;
using YuuzleComic.Common;
using YuuzleComic.DBUtility;
using System.IO;

public partial class admin_comic_add : System.Web.UI.Page
{
    protected string ninfo = "";
    protected YuuzleComic.Model.comics comic = new YuuzleComic.Model.comics();
    protected string zzmc = "";
    YuuzleComic.BLL.zz zzbll = new YuuzleComic.BLL.zz();
    YuuzleComic.BLL.zj zjbll = new YuuzleComic.BLL.zj();
    YuuzleComic.BLL.comics comicbll = new YuuzleComic.BLL.comics();
    YuuzleComic.BLL.ycclass classbll = new YuuzleComic.BLL.ycclass();
    YuuzleComic.BLL.diqu diqubll = new YuuzleComic.BLL.diqu();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            if (Request["act"] == "save")
            {
                if (Request.Form["id"] != "0")
                {
                    comic = comicbll.GetModel(Convert.ToInt32(Request.Form["id"]));
                }
                comic.mhmc=Request.Form["mhmc"];
                comic.zzid = zzbll.GetIdByName(Request.Form["zzmc"].Trim());
                comic.zm=Request.Form["zm"];
                comic.zt = Convert.ToBoolean(Convert.ToInt32(Request.Form["zt"]));
                comic.mhfm=Request.Form["mhfm"];
                comic.mhjs = HttpUtility.HtmlEncode(Request.Form["mhjs"]);
                comic.dqid = Convert.ToInt32(Request.Form["dqid"]);
                comic.classid = Convert.ToInt32(Request.Form["classid"]);
                comic.tj = Convert.ToBoolean(Convert.ToInt32(Request.Form["tj"]));
                comic.mhbm = Request.Form["mhbm"];
                comic.mhtc = Request.Form["mhtc"];
                comic.zzid = 0;
                if (!string.IsNullOrEmpty(Request.Form["zzmc"].Trim()))
                {
                    comic.zzid = zzbll.GetIdByName(Request.Form["zzmc"].Trim());
                    if (comic.zzid == 0)
                    {
                        YuuzleComic.Model.zz zz = new YuuzleComic.Model.zz();
                        zz.zzmc = Request.Form["zzmc"].Trim();
                        zz.dqid = comic.dqid;
                        zz.zztp = "none.jpg";
                        zz.tj = comic.tj;
                        zz.zm = Pinyin.GetInitials(zz.zzmc).Substring(0, 1);
                        zzbll.Add(zz);
                        comic.zzid = zzbll.GetIdByName(zz.zzmc);
                    }
                }
                if (string.IsNullOrEmpty(comic.zm))
                {
                    comic.zm = Pinyin.GetInitials(comic.mhmc).Substring(0, 1);
                }
                if (Request.Form["id"] == "0")
                {
                    comic.adddate = DateTime.Now;
                    comic.mhjs = YuuzleComic.Common.Input.getComicContent(comic.mhjs, DbHelperSQL.Query("select top(1) text from segment order by newid()").Tables[0].Rows[0][0].ToString());
                    comic.id = comicbll.Add(comic);
                }
                else
                {
                    comic.id = Convert.ToInt32(Request.Form["id"]);
                    comicbll.Update(comic);
                }
                this.process_mhfm(comic);
                this.process_mhzj(comic);
                Response.Write("<script>alert('成功：保存漫画资料成功!现在返回管理');location='comics_manage.asp'</script>");
                Response.End();
            }

            classid.DataSource = classbll.GetAllList();
            classid.DataBind();
            dqid.DataSource = diqubll.GetAllList();
            dqid.DataBind();
            if (Request["act"] == "edit")
            {
                ninfo = "修改";
                comic = comicbll.GetModel(Convert.ToInt32(Request["id"]));
                YuuzleComic.Model.zz zz = zzbll.GetModel(comic.zzid);
                if (zz != null)
                {
                    zzmc = zz.zzmc;
                }
                classid.Value = comic.classid.ToString();
                dqid.Value = comic.dqid.ToString();
            }
            else
            {
                ninfo = "添加";
            }
        }
    }

    private void process_mhfm(YuuzleComic.Model.comics comic)
    {
        if (comic.mhfm.Contains("http://"))
        {
            string filename = comic.id + ".jpg";
            WebClient wc = new WebClient();
            byte[] b = wc.DownloadData(comic.mhfm);
            System.IO.MemoryStream ms = new System.IO.MemoryStream(Input.ResizeImageFile(b, 120, 160));
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
            img.Save(Server.MapPath("~/cpic/" + filename), System.Drawing.Imaging.ImageFormat.Jpeg);
            DbHelperSQL.ExecuteSql("update comics set mhfm='" + filename + "' where id=" + comic.id);
        }
    }

    private void process_mhzj(YuuzleComic.Model.comics comic)
    {
        string comicdir = Request.Form["comicdir"].Trim();
        if (!string.IsNullOrEmpty(comicdir) && Directory.Exists(Server.MapPath("~/comicdata/" + comicdir)))
        {
            DirectoryInfo cdir = new DirectoryInfo(Server.MapPath("~/comicdata/" + comicdir));
            foreach (DirectoryInfo zdir in cdir.GetDirectories())
            {
                List<string> imglist = new List<string>();
                foreach (FileInfo f in zdir.GetFiles())
                {
                    if (f.Name != "Thumbs.db")
                    {
                        imglist.Add(f.Name);
                    }
                }
                YuuzleComic.Model.zj zj = new YuuzleComic.Model.zj();
                zj.time = DateTime.Now;
                zj.cid = comic.id;
                zj.isdownload = true;
                zj.tpurl = string.Join("|", imglist.ToArray());
                if (zdir.Name.ToLower().Contains("act_"))
                {
                    zj.zjmc = "第" + zdir.Name.Split('_')[1] + "话";
                }
                else if (zdir.Name.ToLower().Contains("vol_"))
                {
                    zj.zjmc = "第" + zdir.Name.Split('_')[1] + "卷";
                }
                else
                {
                    zj.zjmc = zdir.Name;
                }
                zj.tpadd = comicdir + zdir.Name + "/";
                zj.tps = imglist.Count;
                if (!zjbll.ExistsAdd(zj.tpadd, zj.cid))
                {
                    zjbll.Add(zj);
                }
            }
        }
    }
}